<?php

namespace app\backend\controller;

use think\Controller;
use think\Request;
use think\Session;
// use think\Db;
use app\backend\model\Article as ArticleModel;

class Article extends Controller
{
    function __construct(){

    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        // 当前请求的页数
        $page = $request->param('page');
        if (empty($page)){
            $page = 1;
        }
        // 文章总数
        $article_count = ArticleModel::count();  
        // 每页显示多少条
        $limit = 10;
        // 偏移量  从哪一条开始算起
        $offset = ($page - 1) * $limit;
        // 总页数
        $page_count = intval(ceil($article_count/$limit));
        //SQL  
        $res_articles = ArticleModel::order('created_at desc')->limit($offset, $limit)->select();
        $res = [$res_articles, $page_count, $article_count, $limit];
        return json_encode($res);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
        return 'create';
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $title = $request->param('title');
        $category = $request->param('category');
        $body = $request->param('body');
        $article = new ArticleModel;
        $article->title = $title;
        $article->category = $category;
        $article->body = $body;
        $article->user_id = Session::get('username');
        if($article->save()){
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        return 'read';
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
        if(Session::has('username')){
        }else{
            return 'You Cant Not Found.';
            exit;
        }
        $article = ArticleModel::get($id);
        return json_encode($article);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // if (Session::has('username')){
        //     if(Session::get('permission') != '管理员'){
        //         return 0;
        //         exit;
        //     }
        // }
        $title = $request->param('title');
        $category = $request->param('category');
        $body = $request->param('content');
        if ($title == null || $category == null){
            return 2;
        }
        $article = ArticleModel::get($id);
        $article->title = $title;
        $article->category = $category;
        if (!empty($body)){
            $article->body = $body;
        }
        if($article->save()){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(Session::has('username')){
            if (Session::get('permission') == '管理者'){
            $res = ArticleModel::destroy($id);
                if ($res){
                    return '删除文章成功！';
                }
            }
        }else{
            return 0;
        } 
    }

}
